<?
/**
 * 检测套票合法性
 * User: BaoChun Tong
 * Date: 2014-09-04
 * Time: 上午9:32
 */
 
// 判断套票是否合法 
// $flag 合法即移除提示信息
function isEffectivePack($lid,$DB,$flag=false){
	
	// 获取 attribute
	$sql="SELECT attribute FROM uu_land WHERE status=2 AND id=$lid LIMIT 1";
	$DB->query($sql);
	$DB->fetch_assoc();
	$attr=json_decode($DB->f('attribute'));
	$reqState = $pidArr = array();
	foreach($attr as $v){
		$pidArr[]=$v->pid;
	}
	$reIds = implode(',',$pidArr);
	// 判断是否所有的门票是否上架
	$reCount = count($pidArr);// 关联门票数量
	$sql="SELECT p.id FROM uu_products p LEFT JOIN uu_jq_ticket t ON t.pid=p.id WHERE p.apply_limit=1 AND p.p_status<6 AND p.id IN ($reIds)";
	$sales = array();
	$DB->query($sql);
	while($row = $DB->fetch_assoc()){
		$sales[]=$row;
	}
	if($reCount!=count($sales))return false;//门票中有未上架
	$sql="SELECT * FROM uu_jq_ticket WHERE pid IN ($reIds)";
	$DB->query($sql);
	while($row = $DB->fetch_assoc()){
		$proArr[]=$row;
	}
	$reqState = count(chkTicketAttr($proArr))?false:true;
	
	if($reqState && $flag) removeMsg($lid);
	return $reqState;
}

// 移除 tarMessage.php 通知信息，一般在套票上架属性没有问题时
function removeMsg($lid){
	$file = '/var/www/html/new/d/module/link_product/tarMessage.php';
	if(file_exists($file)){
		$tmp = include $file;
		unset($tmp['S:'.$lid]);
		$saveArr = "<?php\n/*\nStatement：本文档自动生成。关联套票的产品修改后，如果与该套票其他产品冲突，生成下架提示套票供应商。\n*/\nreturn " . var_export($tmp, true)."\n?>";
			writeMessage($saveArr);
	}
}



function chkPackage_new($pid){
	$tarArr = getMainId($pid);// 获取该门票被打包的景区ID
	if(count($tarArr)){
		$reqState = array();
		$packge = new PackTicket();
		foreach($tarArr as $landid){
			$packge->childTicketData($landid);
			$effective = $packge->checkEffectivePack();
			if($effective) continue;
			$reqState['S:'.$landid] = array(
				'timer'=>date('Y年m月d日 H:i:s'),
				'message'=>'由于套票的关联产品被修改导致支付方式或有效期冲突，系统给予下架。',
			);
		}
		
		$tmp = $reqState;
		$file = '/var/www/html/new/d/module/link_product/tarMessage.php';
		if(file_exists($file)){
			$tmp = include $file;
			$tmp = is_array($tmp)? array_merge($tmp,$reqState):$reqState;
		}
		$saveArr = "<?php\n/*\nStatement：本文档自动生成。关联套票的产品修改后，如果与该套票其他产品冲突，生成下架提示套票供应商。\n*/\nreturn " . var_export($tmp, true)."\n?>";
		writeMessage($saveArr);
		return $reqState;		
	}	
}




// tid 是门票的 pid
function chkPackage($tid,$DB){
	$tarArr = getMainId($tid);// 获取该门票被打包的景区ID
	// $tarArr = array(2812, 2813);
	if(count($tarArr)){// 有关联，获取在售的
		$sql="SELECT id,attribute FROM uu_land WHERE status=1 AND id IN (".implode(',',$tarArr).")";
		$DB->query($sql);
		
		// 获取套票的landid
		$lands = array();
		while($row=$DB->fetch_assoc()){
			$lands[$row['id']] = json_decode($row['attribute']);
		};

		// 获取被打包的子票信息
		$reqState = array();
		foreach($lands as $k=>$v){
			$pidArr = array();
			foreach($v as $val){
				$pidArr[]=$val->pid;
			}
			$sql="SELECT * FROM uu_jq_ticket WHERE pid IN (".implode(',',$pidArr).")";
			$DB->query($sql);
			$proArr = array();
			while($row = $DB->fetch_assoc()){
				$proArr[]=$row;
			}

			$tarMsg = chkTicketAttr($proArr);
			if(count($tarMsg)){
				$reqState['S:'.$k] = array(
					'timer'=>date('Y年m月d日 H:i:s'),
					'message'=>'由于套票的关联产品被修改导致支付方式或有效期冲突，系统给予下架。',
				);
			}
		}
		$tmp = $reqState;
		$file = '/var/www/html/new/d/module/link_product/tarMessage.php';
		if(file_exists($file)){
			$tmp = include $file;
			$tmp = is_array($tmp)? array_merge($tmp,$reqState):$reqState;
		}
		$saveArr = "<?php\n/*\nStatement：本文档自动生成。关联套票的产品修改后，如果与该套票其他产品冲突，生成下架提示套票供应商。\n*/\nreturn " . var_export($tmp, true)."\n?>";
			writeMessage($saveArr);
		return $reqState;
	}
}
// 生成提示信息
function buildMess(Array $reqState){
	$tmp = array();
	$file = '/var/www/html/new/d/module/link_product/tarMessage.php';
	if(file_exists($file)){
		$tmp = include $file;
		$tmp = is_array($tmp)? array_merge($tmp,$reqState):$reqState;
	}
	$saveArr = "<?php\n/*\nStatement：本文档自动生成。关联套票的产品修改后，如果与该套票其他产品冲突，生成下架提示套票供应商。\n*/\nreturn " . var_export($tmp, true)."\n?>";
		writeMessage($saveArr);
}

// 检查套票关联的门票属性之间的冲突性，返回错误信息
function chkTicketAttr($list){
	$pInfoArr = $list;
	// 验证之间的属性
	$errMsg=array();
	$payModel = $pInfoArr[0]['pay'];// 支付方式
	$advance = $pInfoArr[0]['ddays'];// 提前购买天数
	$effective = $pInfoArr[0]['ddays']+$pInfoArr[0]['delaydays'];// 有效期最小值
	$priceArr = array();// 价格存储数组
	$minActiveArr = array();// 存储有效期
	foreach($pInfoArr as $k=>$val){
		if($payModel!=$val['pay']){	$errMsg[]='支付方式不一致';break;}
		
		//提前天数不一致
		if($val['ddays'] && ($advance<$val['ddays'])) $advance = $val['ddays'];
		
		$active =  $val['ddays']+$val['delaydays']; // 当前票有效期
		$minActiveArr[] = $active;
		if($active < $effective) $effective = $active;
		// 判断提前天数大于有效期，那么就有一张不能用
		if($advance>$effective){
			$errMsg[]='套票产品须提前'.$advance.'天，有部分产品失效。';break;
		}		
	}

	$pos = array_search(min($minActiveArr), $minActiveArr);
	// 能提前的天数
	// echo $advance;
	// echo $minActiveArr[$pos];
	$minActive = $minActiveArr[$pos]-$advance;
	return $errMsg;
}

// 生成提醒套票供应商数据
function writeMessage($txt,$file="/var/www/html/new/d/module/link_product/tarMessage.php"){
	$fp = fopen($file,"w");
	flock($fp, LOCK_EX);
	fwrite($fp,$txt);
	flock($fp, LOCK_UN);
	fclose($fp);
}
?>